import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/my/sale_center/coupon_pack_logic.dart';
import 'package:sports_new/entity/pay/coupon_pack_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/circular.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';
import 'package:sports_new/widgets/tip_widget.dart';
import '../../../utils/user.dart';
import '../../../widgets/base/qkd_text.dart';
import '../../home/qkd_carousel_view.dart';

class CouponPackPage extends StatefulWidget {
  const CouponPackPage({super.key});

  @override
  State<CouponPackPage> createState() => _CouponPackPageState();
}

class _CouponPackPageState extends State<CouponPackPage>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetBuilder<CouponPackLogic>(
        init: CouponPackLogic(),
        builder: (logic) {
          return Circular(
            gradient: const LinearGradient(
                colors: [Colours.white, Colours.greyF2F3F7],
                stops: [0, 0.2],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter),
            child: LoadingWidget<bool>(
              isLoading: logic.entity == null,
              data: logic.entity?.length == 0,
              child: () => ScrollConfiguration(
                behavior: const ScrollBehavior().copyWith(overscroll: false),
                child: MediaQuery.removePadding(
                  context: context,
                  removeTop: true,
                  child: ListView(children: [
                    if (logic.carousel.hasValue)
                      QkdCarouselView(
                        carousels: logic.carousel!,
                        padding: 8,
                        needTitle: false,
                        aspectRatio: 359 / 120,
                        needDot: (logic.carousel?.length ?? 0) > 1,
                      ).paddingOnly(top: 8),
                    Circular(
                        margin: const EdgeInsets.fromLTRB(8, 8, 8, 0),
                        padding: const EdgeInsets.fromLTRB(16, 16, 16, 0),
                        color: Colours.white,
                        radius: 8,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            const QkdText("优惠购券",
                                fontWeight: FontWeight.w500, fontSize: 16),
                            ...List.generate(logic.entity?.length ?? 0,
                                    (index) => couponItem(logic.entity![index]))
                                .insertEvery(const Divider(
                                    height: 0.5, color: Colours.greyEE))
                          ],
                        )),
                    Container(height: 16),
                    const QkdText("优惠券包规则",
                            fontSize: 12,
                            fontWeight: FontWeight.w500,
                            color: Colours.grey66)
                        .paddingSymmetric(horizontal: 8),
                    Container(height: 8),
                    const TipWidget(type: "coupon_pack")
                        .paddingSymmetric(horizontal: 8),
                    Container(height: 40)
                  ]),
                ),
              ),
            ),
          );
        });
  }

  Widget couponItem(CouponPackEntity coupon) {
    return GetBuilder<CouponPackLogic>(builder: (logic) {
      return SizedBox(
        height: 108,
        child: Row(
          children: [
            Stack(
              children: [
                Image.asset(Utils.getImgPath("coupon_pack.png"),
                    width: 76, height: 78),
                Positioned(
                  top: 3,
                  left: 0,
                  right: 0,
                  bottom: 28,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      QkdText(coupon.couponDeduct?.split("折").join(),
                          fontSize: 24,
                          color: Colours.main,
                          fontWeight: FontWeight.w500,
                          fontFamily: "Din",
                          height: 1),
                      if (coupon.couponDeduct?.contains("折") ?? false)
                        const QkdText("折", color: Colours.main),
                      QkdText("x${coupon.couponCount ?? 0}张",
                          color: Colours.main)
                    ],
                  ),
                ),
                Positioned(
                    bottom: 6,
                    top: 60,
                    left: 0,
                    right: 0,
                    child: QkdText(coupon.couponRequire,
                            needCenter: true,
                            color: Colours.white,
                            fontSize: 12)
                        .center)
              ],
            ),
            Container(width: 10),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  QkdText("${coupon.name}x${coupon.couponCount ?? 0}张",
                      fontSize: 15, fontWeight: FontWeight.w500, maxLines: 2),
                  QkdText(coupon.content,
                      fontSize: 12, color: Colours.grey66, maxLines: 1),
                  Container(height: 11),
                  Row(
                    children: [
                      QkdText("${(coupon.price ?? 0).toStringAsFixed(2)}球币",
                          color: Colours.main, fontWeight: FontWeight.w500),
                      Container(width: 4),
                      if (coupon.priceOld != null)
                        QkdText(
                          "${(coupon.priceOld ?? 0).toStringAsFixed(0)}球币",
                          decoration: TextDecoration.lineThrough,
                          fontSize: 10,
                          color: Colours.grey99,
                        ),
                      const Spacer(),
                      GestureDetector(
                        onTap: () async {
                          if (!User.isLogin) {
                            await User.needLogin(() => logic.getData());
                            return;
                          }
                          if (!coupon.nextPayTime.hasValue) logic.toPay(coupon);
                        },
                        child: Circular(
                            width: 92,
                            height: 30,
                            radius: 18,
                            alignment: Alignment.center,
                            color: coupon.nextPayTime.hasValue
                                ? Colours.grey99
                                : null,
                            gradient: coupon.nextPayTime.hasValue
                                ? null
                                : const LinearGradient(
                                    colors: [
                                      Color(0xFFFF649C),
                                      Color(0xFFFF4667),
                                      Color(0xFFFF5454),
                                      Color(0xFFFF8064)
                                    ],
                                  ),
                            child: QkdText(
                              coupon.nextPayTime.hasValue
                                  ? "${DateTime.parse(coupon.nextPayTime!).month}月${DateTime.parse(coupon.nextPayTime!).day}号可购"
                                  : '立即抢购',
                              fontSize: 13,
                              fontWeight: FontWeight.w500,
                              color: Colours.white,
                              needCenter: true,
                            )),
                      )
                    ],
                  )
                ],
              ),
            ),
          ],
        ),
      );
    });
  }

  @override
  bool get wantKeepAlive => true;
}
